package console;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;

public class RightPanel extends JPanel {

	/** serialization uid */
	private static final long serialVersionUID = 2191989841762172315L;
	private Box centerBox = null;

	private JPanel topRMSPanel = null;

	private JPanel bottomEDFPanel = null;
	private int dlugoscPrc;

	public RightPanel() {
		centerBox = Box.createVerticalBox();
		
		createTopRMSPanel();
		createBottomEDFPanel();

		//addAxisesToTop(5);
		this.setLayout(new BorderLayout());
		this.add(centerBox, BorderLayout.CENTER);

	}

	private void addAxisesToTop(int nr_of, int tab[][], List<Color> colors) {
		for (int i = 0; i < nr_of; i++) {
			topRMSPanel.add(new Axis(dlugoscPrc, 0, 15, 5, 10, tab[i], colors.get(i)));
		}
		this.updateUI();
	}
	
	private void addAxisesToBottom(int nr_of, int tab[][], List<Color> colors){
		for (int i = 0; i < nr_of; i++) {
			bottomEDFPanel.add(new Axis(dlugoscPrc, 0, 15, 5, 10, tab[i], colors.get(i)));
		}

		this.updateUI();
	}
	
	public void clearAxises(){
		topRMSPanel.removeAll();
		bottomEDFPanel.removeAll();
	}
	
	public void paintGanttDiagram(int[][] rmsTab, int[][] edfTab, int dlugoscProcesow, int iloscProcesow){
		this.dlugoscPrc = dlugoscProcesow;
		List<Color> colors = generateColors();
		addAxisesToTop(iloscProcesow, rmsTab, colors);
		addAxisesToBottom(iloscProcesow, edfTab, colors);
	}
	
	private List<Color> generateColors(){
		List<Color> col = new ArrayList<Color>();
		col.add(Color.red);
		col.add(Color.blue);
		col.add(Color.green);
		col.add(Color.magenta);
		col.add(Color.orange);
		col.add(Color.yellow);
		col.add(Color.lightGray);
		col.add(Color.cyan);
		col.add(Color.pink);
		return col;
	}

	private void createTopRMSPanel() {
		TitledBorder title;
		title = BorderFactory.createTitledBorder("RMS");
		topRMSPanel = new JPanel(new GridLayout(9, 1));
		topRMSPanel.setBorder(title);

		topRMSPanel.setBackground(Color.white);
		topRMSPanel.setForeground(Color.white);
		centerBox.add(new JScrollPane(topRMSPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
	}

	private void createBottomEDFPanel() {
		bottomEDFPanel = new JPanel(new GridLayout(9, 1));

		TitledBorder title2;
		title2 = BorderFactory.createTitledBorder("EDF");

		bottomEDFPanel.setBorder(title2);
		bottomEDFPanel.setBackground(Color.white);
		bottomEDFPanel.setForeground(Color.white);

		centerBox.add(new JScrollPane(bottomEDFPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
	}
}
